#!/system/bin/sh
# Project WIPE https://github.com/yc9559/cpufreq-interactive-opt
# Author: yc9559
# Generated at: Thu May 10 14:07:45 2018

# $1:value $2:file path
function set_value() {
	if [ -f $2 ]; then
		chown 0.0 $2
		chmod 0666 $2
		echo $1 > $2
		chmod 0444 $2
	fi
}

# $1:cpu0 $2:timer_rate $3:value
function set_param() {
	echo $3 > /sys/devices/system/cpu/cpufreq/interactive/$2
}

# $1:io-scheduler $2:block-path
function set_io() {
	if [ -f $2/queue/scheduler ]; then
		if [ `grep -c $1 $2/queue/scheduler` = 1 ]; then
			echo $1 > $2/queue/scheduler
			echo 512 > $2/queue/read_ahead_kb
			set_value 0 $2/queue/iostats
			set_value 256 $2/queue/nr_requests
			set_value 0 $2/queue/iosched/slice_idle
		fi
	fi
}

action=$1
setenforce 0

# RunOnce
if [ ! -f /dev/project_wipe_runonce ]; then
	# make sure that sysfs is RW
	mount -o remount,rw sysfs /sys
fi

chown 0.0 /sys/devices/system/cpu/cpufreq/interactive/*
chmod 0666 /sys/devices/system/cpu/cpufreq/interactive/*

# RunOnce
if [ ! -f /dev/project_wipe_runonce ]; then
	# set flag
	touch /dev/project_wipe_runonce

	# Perfd, nothing to worry about, if error the script will continue
	stop perfd

	set_value 90 /proc/sys/kernel/sched_spill_load
	set_value 0 /proc/sys/kernel/sched_boost
	set_value 1 /proc/sys/kernel/sched_prefer_sync_wakee_to_waker
	set_value 40 /proc/sys/kernel/sched_init_task_load
	set_value 3000000 /proc/sys/kernel/sched_freq_inc_notify
	
	set_value 2-3 /dev/cpuset/background/cpus
	set_value 2-3 /dev/cpuset/system-background/cpus
	set_value 0-2,4-7 /dev/cpuset/foreground/cpus
	set_value 0-2,4-7 /dev/cpuset/top-app/cpus

	set_value 25 /proc/sys/kernel/sched_downmigrate
	set_value 45 /proc/sys/kernel/sched_upmigrate

	set_value 80 /sys/module/cpu_boost/parameters/input_boost_ms
	set_value 0 /sys/module/msm_performance/parameters/touchboost

	set_value 580000 /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq

	set_io cfq /sys/block/mmcblk0

	# shared interactive parameters
	set_param cpu0 timer_rate 20000
	set_param cpu0 timer_slack 180000
	set_param cpu0 io_is_busy 0
	set_param cpu0 use_sched_load 1
	set_param cpu0 ignore_hispeed_on_notif 0
	set_value 0 /sys/devices/system/cpu/cpufreq/interactive/enable_prediction
fi

if [ "$action" = "powersave" ]; then
	set_value "0:980000" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 boost 0
	set_param cpu0 above_hispeed_delay "38000 1380000:18000 1680000:58000 1780000:138000"
	set_param cpu0 hispeed_freq 980000
	set_param cpu0 go_hispeed_load 99
	set_param cpu0 boostpulse_duration 78000
	set_param cpu0 target_loads "80 980000:59 1380000:98"
	set_param cpu0 min_sample_time 18000
fi

if [ "$action" = "balance" ]; then
	set_value "0:980000" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 boost 0
	set_param cpu0 above_hispeed_delay "38000 1380000:18000 1680000:58000 1780000:138000"
	set_param cpu0 hispeed_freq 980000
	set_param cpu0 go_hispeed_load 90
	set_param cpu0 boostpulse_duration 78000
	set_param cpu0 target_loads "80 980000:58 1380000:82 1680000:98"
	set_param cpu0 min_sample_time 18000
fi

if [ "$action" = "performance" ]; then
	set_value "0:980000" /sys/module/cpu_boost/parameters/input_boost_freq
	set_param cpu0 boost 0
	set_param cpu0 above_hispeed_delay "38000 1680000:58000 1780000:38000"
	set_param cpu0 hispeed_freq 980000
	set_param cpu0 go_hispeed_load 99
	set_param cpu0 boostpulse_duration 78000
	set_param cpu0 target_loads "80 980000:59 1380000:63 1680000:98"
	set_param cpu0 min_sample_time 38000
fi

if [ "$action" = "fast" ]; then
	set_param cpu0 boost 1
	set_param cpu0 above_hispeed_delay "18000"
	set_param cpu0 hispeed_freq 1680000
	set_param cpu0 target_loads "90 1980000:95"
fi

chmod 0444 /sys/devices/system/cpu/cpufreq/interactive/*

exit 0